package org.csu.linkgame.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import org.csu.linkgame.domain.User;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Comparator;
import java.util.ResourceBundle;

@FXMLController
public class RankViewController implements Initializable {
    @FXML
    public TableColumn username;
    @FXML
    public TableColumn user_goal;
    @FXML
    public TableColumn user_rank;
    @FXML
    public TableView tableView;
    @FXML
    public Button searchButton;
    @FXML
    public TextField input;
    SortedList<User> sortedList;
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/super_link?useUnicode=true&characterEncoding=utf8";


    // 数据库的用户名与密码，需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "2003052288mjp";
    final ObservableList<User> data = FXCollections.observableArrayList();
    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
    public void initializeData() throws Exception{
        // 进行数据绑定
        username.setCellValueFactory(
                new PropertyValueFactory<User,String>("username")
        );
        user_goal.setCellValueFactory(
                new PropertyValueFactory<User,String>("userGoal")
        );
        user_rank.setCellValueFactory(
                new PropertyValueFactory<User,String>("userRank")
        );
        // 连接数据库
        Connection conn = null;
        Statement stmt = null;
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL,USER,PASS);

        stmt = conn.createStatement();
        String sql;
        //sql = "select username,user_goal,user_rank from user";
        //sql = "select * from user order by user_rank";
        sql = "select * from user";
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
            data.add(new User(rs.getString(1),rs.getString(2)
            ,rs.getString(3),rs.getInt(4),rs.getString(5)
            ,rs.getString(6),rs.getString(7),rs.getString(8)));
        }
        SortedList<User> sl=data.sorted(new Comparator<User>() {//干嘛的我也不知道 官方给的方法  排序用

            @Override
            public int compare(User o1, User o2) {
                int a=Integer.parseInt(o1.getUserRank());//从小到大排序
                int b=Integer.parseInt(o2.getUserRank());
                return a-b;
            }
        });
        this.sortedList = sl;
        tableView.setItems(this.sortedList);
        // 完成后关闭
        conn.close();
        stmt.close();

    }
    public void onSearchButtonClicked(MouseEvent mouseEvent) throws Exception {
        //this.sortedList.clear();
        data.clear();
        String username = input.getText();
        // 连接数据库
        Connection conn = null;
        Statement stmt = null;
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL,USER,PASS);

        stmt = conn.createStatement();
        String sql;
        //sql = "select username,user_goal,user_rank from user";
        //sql = "select * from user order by user_rank";
        if(username.equals("")){
            sql = "select * from user";
        }else{
            sql = "select * from user where username =\""+username+"\"";
        }
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
            data.add(new User(rs.getString(1),rs.getString(2)
                    ,rs.getString(3),rs.getInt(4),rs.getString(5)
                    ,rs.getString(6),rs.getString(7),rs.getString(8)));
        }
        SortedList<User> sl=data.sorted(new Comparator<User>() {//干嘛的我也不知道 官方给的方法  排序用
            @Override
            public int compare(User o1, User o2) {
                int a=Integer.parseInt(o1.getUserRank());//从小到大排序
                int b=Integer.parseInt(o2.getUserRank());
                return a-b;
            }
        });
        tableView.setItems(sl);
        // 完成后关闭
        conn.close();
        stmt.close();
    }
}
